<template>
	<uni-popup ref="popup" type="bottom">
		<view class="content">
			<view class="hd">
				<text>打招呼语设置</text>
				<icon class="iconfont icon" @click="close()">&#xe733;</icon>
			</view>
			<view class="bd">
				<view class="item-list">
					<view
						class="item"
						:class="assistSetData.selectIndex === index ? 'active' : ''"
						v-for="(item, index) in assistSetData.greetingsItem"
						:key="index"
						@click="selectGreetingsType(index)"
					>
						{{ item }}
					</view>
				</view>
				<view class="greet-list" v-if="!assistSetData.isShowTextArae">
					<view class="checkbox-group" v-for="(item, index) in assistSetData.greetingsList" :key="index">
						<radio-group class="checkbox" @change="selectCheckBox(item, index)">
							<text class="text">{{ item }}</text>
							<radio value="item" :checked="assistSetData.selectCheckBoxIndex === index" color="#E1835B" />
						</radio-group>
					</view>
				</view>
				<view class="custom-text-area" v-if="assistSetData.isShowTextArae">
					<textarea
						placeholder="请输入您要打招呼的内容（如：能提供的服务范围、优势介绍、彰显知识服务专 业度的语句）"
						v-model="assistSetData.customGreets"
						class="custom-greets"
						maxlength="120"
						placeholder-class="placeholderStyle"
					></textarea>
					<view class="length">{{ assistSetData.customGreets.length }}/120</view>
				</view>
			</view>

			<view class="ft">
				<radio-group class="checkbox" @click="autoSendChange">
					<radio color="#E1835B" :checked="assistSetData.defaultgreetings === 1" style="transform:scale(0.7)" />
					<text>点击打招呼时直接发送</text>
				</radio-group>
				<button class="btn btn-sub" hover-class="btnClickStyle" @click="pushGreetsRequest">确定</button>
			</view>
		</view>
	</uni-popup>
</template>

<script setup>
import { ref, reactive } from 'vue';
import { greetingsLan, greetingsSet } from '@/subpages/common/api/set.js';

const popup = ref(null);
const emits = defineEmits(['assistSetCallback']);
const assistSetData = reactive({
	greetingsItem: ['常规', '幽默', '诚恳', '自定义'],
	selectIndex: 0,
	selectCheckBoxIndex: 0,
	greetingsAllList: [],
	greetingsList: [],
	isShowTextArae: false,
	customGreets: '',
	defaultgreetings: 1
});

defineExpose({
	open,
	close
});

function open() {
	popup.value.open();
	greetingsLanRequest();
}
//获取打招呼语列表
async function greetingsLanRequest() {
	const res = await greetingsLan();
	for (let item in res.data) {
		assistSetData.greetingsAllList.push(res.data[item]);
	}
	selectGreetingsType(0);
}
//选择打招呼类型
function selectGreetingsType(index) {
	assistSetData.selectIndex = index;
	if (index === 3) {
		assistSetData.isShowTextArae = true;
		assistSetData.customGreets = '';
	} else {
		assistSetData.isShowTextArae = false;
		assistSetData.selectCheckBoxIndex = 0;
		assistSetData.greetingsList = assistSetData.greetingsAllList[index];
		selectCheckBox(assistSetData.greetingsAllList[index][0], 0);
	}
}
//选择打招呼语
function selectCheckBox(item, index) {
	assistSetData.customGreets = item;
	assistSetData.selectCheckBoxIndex = index;
}
//是否开启自动打招呼
function autoSendChange() {
	assistSetData.defaultgreetings === 1 ? (assistSetData.defaultgreetings = 0) : (assistSetData.defaultgreetings = 1);
}
//提交打招呼语
async function pushGreetsRequest() {
	if (assistSetData.customGreets) {
		const res = await greetingsSet({ greetings: assistSetData.customGreets, defaultgreetings: assistSetData.defaultgreetings });
		toastLayer('设置成功');
		emits('assistSetCallback', assistSetData.customGreets);
		popup.value.close();
	} else {
		toastLayer('自定义打招呼语不能为空');
	}
}
function close() {
	popup.value.close();
}
function toastLayer(text) {
	uni.showToast({
		icon: 'none',
		title: text
	});
}
</script>

<style scoped lang="scss">
@import '@/common/css/base.scss';
.content {
	width: 100%;
	padding-bottom: 32rpx;
	border-radius: 16rpx;
	border-bottom-left-radius: 0;
	border-bottom-right-radius: 0;
	background-color: #fff;
	.hd {
		@include flex(center, space-between);
		padding: 38rpx 26rpx;
		border-bottom: 1rpx solid #ddd;
		font-size: 36rpx;
		font-weight: bold;
		.icon {
			font-size: 36rpx;
			color: #666;
		}
	}
	.bd {
		padding: 38rpx 28rpx 0 28rpx;
		margin-bottom: 76rpx;
		.item-list {
			@include flex(center, space-between);
			margin-bottom: 50rpx;
			.item {
				display: inline-block;
				width: 156rpx;
				height: 68rpx;
				line-height: 68rpx;
				text-align: center;
				font-size: 26rpx;
				color: #aaa;
				border-radius: 8px;
				background-color: #ececec;
				border: 1px solid #ececec;
			}
			.active {
				color: #e1835b;
				border-color: #e1835b;
				background-color: #fff5e5;
			}
		}
		.greet-list {
			height: 550rpx;
			.checkbox-group {
				margin: 15rpx 0;
				padding: 40rpx 30rpx;
				background: rgba(249, 249, 249, 0.97);
				border: 1rpx solid #ececec;
				border-radius: 16rpx;
				font-size: 26rpx;
				.checkbox {
					@include flex(center, space-between);
				}
				.text {
					display: inline-block;
					width: 85%;
				}
			}
		}
		.custom-text-area {
			position: relative;
			height: 550rpx;
			.custom-greets {
				width: 90%;
				height: 85%;
				padding: 40rpx 5%;
				line-height: 60rpx;
				background: rgba(249, 249, 249, 0.97);
				border: 1rpx solid #ececec;
				border-radius: 16rpx;
			}
			.length {
				position: absolute;
				bottom: 22rpx;
				right: 22rpx;
				color: #888888;
				font-size: 24rpx;
			}
		}
	}
	.ft {
		.checkbox {
			@include flex(center, center);
			margin: 0 auto;
			margin-bottom: 20rpx;
			font-size: 24rpx;
			color: #999;
		}
		.btn-sub {
			width: 695rpx;
			height: 80rpx;
			line-height: 80rpx;
			color: #fff;
			background: #e1835b;
			border-radius: 40rpx;
		}
	}
}
</style>
